import time, sensor, image
from image import SEARCH_EX, SEARCH_DS
from pyb import UART
sensor.reset()
sensor.set_contrast(1)
sensor.set_gainceiling(16)
sensor.set_framesize(sensor.QQVGA)
sensor.set_pixformat(sensor.GRAYSCALE)
template1_1 = image.Image("/1-5.pgm")
template1_2 = image.Image("/1-6.pgm")
template1_3 = image.Image("/1-7.pgm")
template2_1 = image.Image("/2-3.pgm")
template2_2 = image.Image("/2-4.pgm")
template2_3 = image.Image("/2-5.pgm")
template3_1 = image.Image("/3-5.pgm")
template3_2 = image.Image("/3-6.pgm")
template3_3 = image.Image("/3-7.pgm")
template4_1 = image.Image("/4-3.pgm")
template4_2 = image.Image("/4-4.pgm")
template4_3 = image.Image("/4-5.pgm")
template5_1 = image.Image("/5-5.pgm")
template5_2 = image.Image("/5-6.pgm")
template5_3 = image.Image("/5-7.pgm")
template6_1 = image.Image("/6-2.pgm")
template6_2 = image.Image("/6-3.pgm")
template6_3 = image.Image("/6-4.pgm")
template7_1 = image.Image("/7-6.pgm")
template7_2 = image.Image("/7-7.pgm")
template7_3 = image.Image("/7-8.pgm")
template8_1 = image.Image("/8-3.pgm")
template8_2 = image.Image("/8-4.pgm")
template8_3 = image.Image("/8-6.pgm")
clock = time.clock()
uart = UART(3, 115200)
while (True):
	clock.tick()
	img = sensor.snapshot()
	a = img.find_template(template1_1 or template1_2 or template1_3, 0.7, step=2, search=SEARCH_EX)
	if a:
		img.draw_rectangle(a,color=0)
		print("1")
		uart.write('target=1!')
	b = img.find_template(template2_1 or template2_2 or template2_3, 0.7, step=2, search=SEARCH_EX)
	if b:
		img.draw_rectangle(b,color=0)
		print("2")
		uart.write('target=2!')
	c = img.find_template(template3_1 or template3_2 or template3_3, 0.7, step=2, search=SEARCH_EX)
	if c:
		img.draw_rectangle(c,color=0)
		print("3")
		uart.write('target=3!')
	d = img.find_template(template4_1 or template4_2 or template4_3, 0.7, step=2, search=SEARCH_EX)
	if d:
		img.draw_rectangle(d,color=0)
		print("4")
		uart.write('target=4!')
	e = img.find_template(template5_1 or template5_2 or template5_3, 0.7, step=2, search=SEARCH_EX)
	if e:
		img.draw_rectangle(e,color=0)
		print("5")
		uart.write('target=5!')
	f = img.find_template(template6_1 or template6_2 or template6_3, 0.7, step=2, search=SEARCH_EX)
	if f:
		img.draw_rectangle(f,color=0)
		print("6")
		uart.write('target=6!')
	g = img.find_template(template7_1 or template7_2 or template7_3, 0.7, step=2, search=SEARCH_EX)
	if g:
		img.draw_rectangle(g,color=0)
		print("7")
		uart.write('target=7!')
	h = img.find_template(template8_1 or template8_2 or template8_3, 0.7, step=2, search=SEARCH_EX)
	if h:
		img.draw_rectangle(h,color=0)
		print("8")
		uart.write('target=8!')